﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： CarGroupBrand.cs
// 项目名称：斑马车务
// 创建时间：2019/11/11
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Repository;
namespace YouWei.Infrastructure.Service
{
	
	/// <summary>
	///
	/// </summary>
	public class CarGroupBrandService
	{
         protected CarGroupBrandRepository repository;
         
         public  CarGroupBrandService(){
           repository = new CarGroupBrandRepository();         
         }
         
        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(CarGroupBrand entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<CarGroupBrand> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(CarGroupBrand entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(CarGroupBrand entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public CarGroupBrand SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }

       
        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public CarGroupBrand Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<CarGroupBrand> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 根据群组ID获取所对应的品牌列表
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Dictionary<int, List<CarGroupBrand>> GetCarGroupBrandDic(List<int> ids)
        {
            Dictionary<int, List<CarGroupBrand>> dic = new Dictionary<int, List<CarGroupBrand>>();
            List<CarGroupBrand> list = repository.GetCarGroupBrandList(ids);
            List<CarGroupBrand> dicList = null;
            foreach (var item in list)
            {
                if (!dic.ContainsKey(item.CarGroupID))
                {
                    dicList = new List<CarGroupBrand>();
                    dicList.Add(item);
                    dic.Add(item.CarGroupID, dicList);
                }
                else
                {
                    dicList = dic[item.CarGroupID];
                    dicList.Add(item);
                    dic[item.CarGroupID] = dicList;
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取单个群组对应的品牌
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<CarGroupBrand> GetCarGroupBrandList(int id)
        {
            List<CarGroupBrand> list = new List<CarGroupBrand>();
            List<int> ids = new List<int>();
            ids.Add(id);
            Dictionary<int, List<CarGroupBrand>> dic = GetCarGroupBrandDic(ids);
            if (dic.ContainsKey(id))
            {
                list = dic[id];
            }
            return list;
        }


        public CarGroupBrand GetCarGroupBrandByModel(int modelId)
        {
            return repository.GetCarGroupBrandByModel(modelId);
        }

    }
	
}